const config = require('./config/config');
const fs = require('fs');
const path = require('path');
// 引入json解析中间件
const bodyParser = require('body-parser');
const express = require('express');
const app = express();



// app.use(express.json());
// 添加json解析
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));


// mongo数据库连接
const mongodb = require('./config/mongodb');
mongodb.connect();


//mongo路由
var userRouter = require('./api/user')
app.use('/api/user',userRouter)

// mysql后端api路由
// const userApi = require('./api/userApi');
// app.use('/api/user2', userApi);

	
	// 允许所有的请求形式 
app.use(function(req, res, next) { 
	res.header("Access-Control-Allow-Origin", "*"); 
	res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	next();
  });

// error handler
app.use(function(err, req, res, next) {
	
	// set locals, only providing error in development
	res.locals.message = err.message;
	res.locals.error = req.app.get('env') === 'development' ? err : {};

	// render the error page
	res.status(err.status || 500);
	res.render('error');
});
// 监听端口
app.listen(config.server.port);
console.log('服务已启动');
console.log('监听端口 :'+config.server.port);